package com.mldong.modules.sys.vo;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Dict;
import com.mldong.auth.Perm;
import com.mldong.modules.sys.enums.UserAdminTypeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Date;
import java.util.List;

@Data
@ApiModel
public class LoginVO {
    @ApiModelProperty(value = "用户ID")
    private Long userId;
    @ApiModelProperty(value = "用户名")
    private String userName;
    @ApiModelProperty(value = "访问凭证")
    private String token;
    @ApiModelProperty(value = "权限标识集合")
    private List<String> perms;
    @ApiModelProperty(value = "是否超级管理员")
    private boolean superAdmin;
    private UserAdminTypeEnum adminType;
    @ApiModelProperty(value = "扩展信息")
    private Dict ext = Dict.create();
    @ApiModelProperty(value = "角色ID集合")
    private List<Long> roleIdList;
    @ApiModelProperty(value = "角色名称集合")
    private List<String> roleNameList;
    @ApiModelProperty(value = "部门名称")
    private String deptName;
    @ApiModelProperty(value = "上次登录的时间")
    private Date lastLoginTime;
    /**
     * 是否有权限
     * @param perm
     * @return
     */
    public boolean hasPerm(String[] perm,String mode) {
        if(Perm.AND.equalsIgnoreCase(mode)){
            // CollectionUtil.containsAll(coll1,coll2) 只要coll1所有都要在coll2中
            return CollectionUtil.isNotEmpty(perms)
                    && CollectionUtil.containsAll(CollectionUtil.newArrayList(perm), perms);
        } else {
            // CollectionUtil.containsAny(coll1,coll2) 只要coll1其中有一个在coll2中即可
            return CollectionUtil.isNotEmpty(perms)
                    && CollectionUtil.containsAny(CollectionUtil.newArrayList(perm), perms);
        }

    }
}
